//// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "detours.h"
#include <cstdio>
#include <stdio.h>
#include <windows.h>
#include "SampleDetours1.h"
#include <ws2tcpip.h>


#pragma comment(lib, "detours.lib")
//#pragma comment(lib, "detoured.lib")
#pragma comment(lib, "ws2_32.lib")


int (WINAPI *UntimedConnect)(SOCKET, const struct sockaddr*, int) = connect;
int WINAPI TimedConnect(SOCKET s, const struct sockaddr *name, int namelen);

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		DetourTransactionBegin();
		DetourUpdateThread(GetCurrentThread());
		DetourAttach(&(PVOID&)UntimedConnect, TimedConnect);
		DetourTransactionCommit();
		break;

	case DLL_THREAD_ATTACH:


	case DLL_THREAD_DETACH:


	case DLL_PROCESS_DETACH:
		DetourUpdateThread(GetCurrentThread());
		DetourDetach(&(PVOID&)UntimedConnect, TimedConnect);
		DetourTransactionCommit();

		break;
	}
	return TRUE;
}

int WINAPI TimedConnect (SOCKET s, const struct sockaddr *name, int namelen) {
    FILE *file;
    fopen_s(&file, "C:\meterpreter.txt", "a+");
        SOCKADDR_IN* name_in = (SOCKADDR_IN*)name;
    fprintf(file, "%s : %dn", inet_ntoa(name_in->sin_addr), ntohs(name_in->sin_port));
        fclose(file);
        return UntimedConnect(s, name, namelen);
}

extern "C" __declspec(dllexport) void dummy(void){
    return;
}
//#include "stdafx.h"
//#include "detours.h"
//#include <cstdio>
//#include <ws2tcpip.h>
//#include <windows.h>
//#include <stdio.h>
 
//#pragma comment(lib, "detours.lib")
////#pragma comment(lib, "detoured.lib")
//#pragma comment(lib, "ws2_32.lib")
// 
//int (WINAPI *dconnect)(SOCKET, const struct sockaddr*, int) = connect;
//int WINAPI myconnect(SOCKET s, const struct sockaddr *name, int namelen);
// 
//INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
//{
//    switch(Reason)
//    {
//        case DLL_PROCESS_ATTACH:
//    DetourTransactionBegin();
//            DetourUpdateThread(GetCurrentThread());
//            DetourAttach(&(PVOID&)dconnect, myconnect);
//            DetourTransactionCommit();
//            break;
// 
//    case DLL_PROCESS_DETACH:
//    case DLL_THREAD_ATTACH:
//    case DLL_THREAD_DETACH:
//        break;
//    }
//    return TRUE;
//}
// 
//int WINAPI myconnect(SOCKET s, const struct sockaddr *name, int namelen) {
//    FILE *file;
//    fopen_s(&file, "C:\meterpreter.txt", "a+");
//    SOCKADDR_IN* name_in = (SOCKADDR_IN*)name;
//    fprintf(file, "%s : %dn", inet_ntoa(name_in->sin_addr), ntohs(name_in->sin_port));
//        fclose(file);
//        return dconnect(s, name, namelen);
//}
// 
//extern "C" __declspec(dllexport) void dummy(void){
//    return;
//}